package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.AnchorGroup;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IAnchorGroupService;
import com.originskyseed.platform.user.vo.request.GroupRequestVO;

/**
 * 分组信息表Controller类（OSI_ANCHOR_GROUP）
 *
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/anchorGroup")
@BaseLocaleResource(localeFileName = "i18n_user")
public class AnchorGroupController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AnchorGroupController.class);

	@DubboReference
	private IAnchorGroupService groupService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/groupList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<AnchorGroup>> groupList(HttpServletRequest request) {
		BaseResponseBody<BasePage<AnchorGroup>> result = null;
		try {
			// 请求对象
			GroupRequestVO requestVO = this.getParameterBean(GroupRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// requestVO.setCreatorId(userInfo.getId());

			// 分页查询
			BasePage<AnchorGroup> page = new BasePage<AnchorGroup>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.groupService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<AnchorGroup>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<AnchorGroup>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/groupSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<AnchorGroup> groupSave(HttpServletRequest request) {
		BaseResponseBody<AnchorGroup> result = null;
		try {
			// 请求对象
			AnchorGroup anchorGroup = this.getParameterBean(AnchorGroup.class);

			if (StringUtils.isNotBlank(anchorGroup.getId())) {
				this.groupService.updateGroup(anchorGroup);
			} else {
				UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
				anchorGroup.setCreator(userInfo.getName());
				anchorGroup.setCreatorId(userInfo.getId());
				anchorGroup.setIsDel(0);
				Integer prority = this.groupService.selectPrority();
				if (prority == null) {
					anchorGroup.setPriority(0);
				} else {
					anchorGroup.setPriority(prority + 1);
				}
				this.groupService.insertGroup(anchorGroup);
			}
			result = new BaseResponseBody<AnchorGroup>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), anchorGroup);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<AnchorGroup>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/groupDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> groupDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.groupService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/groupDetails.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<AnchorGroup> groupDetails(HttpServletRequest request) {
		BaseResponseBody<AnchorGroup> result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			AnchorGroup anchorGroup = this.groupService.selectByPrimaryKey(id);
			result = new BaseResponseBody<AnchorGroup>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), anchorGroup);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<AnchorGroup>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 修改置顶信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateIsTop.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody updateIsTop(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			AnchorGroup group = new AnchorGroup();
			group.setId(id);
			group.setIsTop(1);
			group.setUpdateTime(new Date());
			this.groupService.updateIsTop(group);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 升序降序
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUpOrDown.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id", "upOrDown", "prority" })
	public BaseResponseBody updateUpAndDown(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);
			Integer upOrDown = this.getParameterInteger("upOrDown", null);
			Integer prority = this.getParameterInteger("prority", null);
			Integer updatePrority = prority;

			// 等于0是升序 1是降序
			if (upOrDown == 0) {
				prority += 1;
			} else {
				prority -= 1;
			}
			// 将另外一个升序或降序
			AnchorGroup anchorGroup = this.groupService.selectGroupByPrority(prority);
			if (anchorGroup != null) {
				anchorGroup.setPriority(updatePrority);
			}

			AnchorGroup updateGroup = new AnchorGroup();
			updateGroup.setId(id);
			updateGroup.setPriority(prority);
			if (anchorGroup != null) {
				this.groupService.updatePrority(anchorGroup, updateGroup);
			} else {
				this.groupService.updateByPrimaryKeySelective(updateGroup);
			}

			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询分组
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectGroupList.json", method = RequestMethod.POST)
	public BaseResponseBody selectGroupList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			AnchorGroup anchorGroup = this.getParameterBean(AnchorGroup.class);

			// 查询列表
			List<AnchorGroup> anchorGroupList = this.groupService.selectList(anchorGroup);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), anchorGroupList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
